package com.getpebble.android.framework.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.framework.comm.MessageRouter;
import com.getpebble.android.framework.endpoint.EndpointRequest;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.getpebble.android.framework.util.PhoneUtil;
import com.google.common.base.Strings;

public class PhoneReceiver extends BroadcastReceiver {
    private static final String TAG = PhoneReceiver.class.getSimpleName();
    private static int sCachedRingerMode = 9999;
    private static byte[] sCookie = null;
    private static PhoneState sPhoneState = PhoneState.IDLE;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$receiver$PhoneReceiver$PhoneState = new int[PhoneState.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$receiver$PhoneReceiver$PhoneState[PhoneState.OFFHOOK.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$receiver$PhoneReceiver$PhoneState[PhoneState.IDLE.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$receiver$PhoneReceiver$PhoneState[PhoneState.RINGING.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public enum PhoneState {
        IDLE,
        RINGING,
        OFFHOOK
    }

    private void onIdle(Context context) {
        synchronized (TAG) {
            PhoneState prevPhonestate = sPhoneState;
            sPhoneState = PhoneState.IDLE;
            restoreRingerMode(context);
            if (PhoneState.IDLE.equals(prevPhonestate)) {
                Trace.debug(TAG, "onIdle(): already idle");
            } else if (sCookie == null) {
                Trace.warning(TAG, "onIdle(): cookie is null");
            } else {
                Bundle args = new Bundle();
                args.putByteArray(EndpointArgumentKeys.PHONE_COOKIE.toString(), sCookie);
                EndpointRequest request = new EndpointRequest(EndpointId.PHONE_CONTROL, EndpointAction.SEND_PHONE_END_NOTIFICATION, args);
                Trace.verbose(TAG, "Sending call end notification");
                sendToConnectedDevices(request);
                sCookie = null;
            }
        }
    }

    private void onRing(String callingNumber, String callingName) {
        synchronized (TAG) {
            PhoneState prevPhonestate = sPhoneState;
            sPhoneState = PhoneState.RINGING;
            if (PhoneState.IDLE.equals(prevPhonestate)) {
                sCookie = ByteUtils.randomCookie();
                Bundle args = new Bundle();
                args.putString(EndpointArgumentKeys.PHONE_NUMBER.toString(), callingNumber);
                args.putString(EndpointArgumentKeys.PHONE_NAME.toString(), callingName);
                args.putByteArray(EndpointArgumentKeys.PHONE_COOKIE.toString(), sCookie);
                EndpointRequest request = new EndpointRequest(EndpointId.PHONE_CONTROL, EndpointAction.SEND_PHONE_INCOMING_CALL_NOTIFICATION, args);
                Trace.verbose(TAG, "Sending incoming call notification");
                sendToConnectedDevices(request);
                return;
            }
            Trace.debug(TAG, "onRing(): not idle");
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private void onOffHook() {
        /*
        r6 = this;
        r4 = TAG;
        monitor-enter(r4);
        r2 = sPhoneState;	 Catch:{ all -> 0x001f }
        r3 = com.getpebble.android.framework.receiver.PhoneReceiver.PhoneState.OFFHOOK;	 Catch:{ all -> 0x001f }
        sPhoneState = r3;	 Catch:{ all -> 0x001f }
        r3 = com.getpebble.android.framework.receiver.PhoneReceiver.AnonymousClass1.$SwitchMap$com$getpebble$android$framework$receiver$PhoneReceiver$PhoneState;	 Catch:{ all -> 0x001f }
        r5 = r2.ordinal();	 Catch:{ all -> 0x001f }
        r3 = r3[r5];	 Catch:{ all -> 0x001f }
        switch(r3) {
            case 1: goto L_0x0016;
            case 2: goto L_0x0014;
            case 3: goto L_0x0022;
            default: goto L_0x0014;
        };	 Catch:{ all -> 0x001f }
    L_0x0014:
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
    L_0x0015:
        return;
    L_0x0016:
        r3 = TAG;	 Catch:{ all -> 0x001f }
        r5 = "onOffHook(): already off hook";
        com.getpebble.android.common.core.trace.Trace.debug(r3, r5);	 Catch:{ all -> 0x001f }
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
        goto L_0x0015;
    L_0x001f:
        r3 = move-exception;
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
        throw r3;
    L_0x0022:
        r3 = sCookie;	 Catch:{ all -> 0x001f }
        if (r3 != 0) goto L_0x002f;
    L_0x0026:
        r3 = TAG;	 Catch:{ all -> 0x001f }
        r5 = "onOffHook(): cookie is null (ringing)";
        com.getpebble.android.common.core.trace.Trace.warning(r3, r5);	 Catch:{ all -> 0x001f }
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
        goto L_0x0015;
    L_0x002f:
        r0 = new android.os.Bundle;	 Catch:{ all -> 0x001f }
        r0.<init>();	 Catch:{ all -> 0x001f }
        r3 = com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys.PHONE_COOKIE;	 Catch:{ all -> 0x001f }
        r3 = r3.toString();	 Catch:{ all -> 0x001f }
        r5 = sCookie;	 Catch:{ all -> 0x001f }
        r0.putByteArray(r3, r5);	 Catch:{ all -> 0x001f }
        r1 = new com.getpebble.android.framework.endpoint.EndpointRequest;	 Catch:{ all -> 0x001f }
        r3 = com.getpebble.android.framework.protocol.EndpointId.PHONE_CONTROL;	 Catch:{ all -> 0x001f }
        r5 = com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction.SEND_PHONE_START_NOTIFICATION;	 Catch:{ all -> 0x001f }
        r1.<init>(r3, r5, r0);	 Catch:{ all -> 0x001f }
        r3 = TAG;	 Catch:{ all -> 0x001f }
        r5 = "Sending call started notification";
        com.getpebble.android.common.core.trace.Trace.verbose(r3, r5);	 Catch:{ all -> 0x001f }
        r6.sendToConnectedDevices(r1);	 Catch:{ all -> 0x001f }
        goto L_0x0014;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.getpebble.android.framework.receiver.PhoneReceiver.onOffHook():void");
    }

    public void onReceive(Context context, Intent intent) {
        if (context == null) {
            Trace.error(TAG, "onReceive: context was null");
        } else if (!new PblPreferences(context).getBooleanData(PrefKey.CALL_NOTIFICATIONS, true)) {
            Trace.verbose(TAG, "Call notifications disabled");
        } else if (intent == null || intent.getAction() == null) {
            Trace.warning(TAG, "Received a null intent in the Phone broadcast receiver");
        } else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
            String callState = intent.getStringExtra("state");
            if (Strings.isNullOrEmpty(callState)) {
                Trace.info(TAG, "Recieved an empty phone state");
            } else if (callState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                Trace.verbose(TAG, "Received 'Idle' event");
                onIdle(context);
            } else if (callState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String callingNumber = intent.getStringExtra("incoming_number");
                String callingName = PhoneUtil.getContactName(context, callingNumber);
                Trace.verbose(TAG, "Received 'Ringing' event callingNumber = " + callingNumber + " callingName = " + callingName);
                onRing(callingNumber, callingName);
            } else if (callState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                Trace.verbose(TAG, "Received 'Offhook' event");
                onOffHook();
            } else {
                Trace.verbose(TAG, "Don't know how to handle this telephony intent: " + intent.toUri(1));
            }
        }
    }

    protected void sendToConnectedDevices(EndpointRequest request) {
        for (MessageRouter router : MessageRouter.getAllRouters()) {
            router.onRequest(request, null);
        }
    }

    private static void restoreRingerMode(Context context) {
        if (sCachedRingerMode != 9999) {
            AudioManager am = (AudioManager) context.getSystemService("audio");
            Trace.debug(TAG, "Restoring ringer mode to " + sCachedRingerMode);
            am.setRingerMode(sCachedRingerMode);
            sCachedRingerMode = 9999;
        }
    }
}
